package fr.androguies.powerball.scene;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.MathUtils;

import fr.androguies.powerball.sprite.Tank;

public class SceneJeu extends Scene {

	private BaseGameActivity context;
	private Texture texture;
	private TextureRegion textureRegionTank;
	private Tank tank;

	private Texture mOnScreenControlTexture;
	private TextureRegion mOnScreenControlBaseTextureRegion;
	private TextureRegion mOnScreenControlKnobTextureRegion;

	private AnalogOnScreenControl analogOnScreenControl;


	public SceneJeu(BaseGameActivity context) {
		this.context = context;
		setBackground(new ColorBackground(0.52f, 0.75f, 0.03f));
	}

	public void loadResources() {
		texture = new Texture(128, 256);
		textureRegionTank = TextureRegionFactory.createFromAsset(texture, context, "tank.png", 0, 0);
		context.getEngine().getTextureManager().loadTextures(texture);

		mOnScreenControlTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, context, "onscreen_control_base.png", 0, 0);
		mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, context, "onscreen_control_knob.png", 128, 0);
		context.getEngine().getTextureManager().loadTextures(texture,mOnScreenControlTexture);

		init();
	}

	private void init() {
		tank = new Tank(0, 0, textureRegionTank.getWidth(), textureRegionTank.getHeight(), textureRegionTank);
		tank.setScale(0.5f);

		final PhysicsHandler handler = new PhysicsHandler(tank);
		tank.registerUpdateHandler(handler);
			
		attachChild(tank);

		final IAnalogOnScreenControlListener listener = new IAnalogOnScreenControlListener() {

			@Override
			public void onControlChange(BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) {
				handler.setVelocity(pValueX * 100, pValueY * 100);
				
				if(pValueX != 0 && pValueY != 0) tank.setRotation(MathUtils.radToDeg((float)Math.atan2(pValueX, -pValueY)));
			}

			@Override
			public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {
				
			}
		};

		analogOnScreenControl = new AnalogOnScreenControl(
				0,
				context.getEngine().getCamera().getHeight() - mOnScreenControlBaseTextureRegion.getHeight(),
				context.getEngine().getCamera(),
				mOnScreenControlBaseTextureRegion,
				mOnScreenControlKnobTextureRegion,
				0.1f,
				200,
				listener
		);

		analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		analogOnScreenControl.getControlBase().setAlpha(0.5f);
		analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
		analogOnScreenControl.getControlBase().setScale(0.5f);
		analogOnScreenControl.getControlKnob().setScale(0.5f);
		analogOnScreenControl.refreshControlKnobPosition();

		setChildScene(analogOnScreenControl);
	}

}
